home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 680 / atap / sourcecode / convertafm.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  5KB  |  186 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <proto/exec.h>
  4.  
  5. #include "AmigaEncoding.h"
  6. /* Amiga standard character encoding array *amigaEncoding[256]. */
  7.  
  8. /* My own functions: */
  9.  
  10. char    *freadline(FILE *sourceFile);
  11. char    *FindBaseName(char *typefaceName);
  12. char    *GetMetricFilename(char typefaceNames[][68]);
  13. ULONG    WriteMetricsVector(char *afmFilename, int typeWeight);
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.     FILE    *afmFiles[4],*metricFile,*metricVectorFile;
  18.     ULONG    metricOffsets[5],vectorLengths[4];
  19.     char    typefaceNames[4][68],vectorFilename[22];
  20.     char    familyName[30],*incomingString,*singleChars,oneChar;
  21.     int    test,index,index2;
  22.  
  23. /* This first part is specific to the CLI/Shell version of ConvertAFM.
  24.    It checks the AFMs' contents to make sure they belong to the same
  25.    typeface family. The program aborts conversion if:
  26.  
  27.    1) The first AFM file isn't found,
  28.    2) One of the files isn't a FontMetrics 2.0 file, or
  29.    3) The files aren't from the same typeface family.
  30.  
  31.    NOTE: The program continues conversion even if one .AFM file
  32.    isn't found (Except for the first). This allows the user to
  33.    include only a plain & italic version, or a plain & bold version,
  34.    or any such combination, so long as they belong to the same
  35.    typeface family.
  36.  
  37.    The third condition is overridden if the user supplies an alternate
  38.    .metric filename. Custom .metric organizations are then possible. */
  39.  
  40.     if (argc<5)
  41.     {
  42.         printf("\nUsage: %s Reg.AFM [Bld.AFM] [Itl.AFM] [BldItl.AFM] [Output.metric]\n",argv[0]);
  43.         printf("\nTo omit an .AFM, insert a dummy filename (eg: none.afm).\nYou MUST give one .AFM for Plain style.\n\n");
  44.         exit(0);
  45.     }
  46.  
  47.     familyName[0]=0;
  48.     if (argc==6)
  49.         strcpy(familyName,argv[5]);
  50.  
  51. /* Start checking the files themselves. Are they FontMetrics 2.0? */
  52.     
  53.     for (index=0;index<4;index++)
  54.         afmFiles[index]=fopen(argv[index+1],"r");
  55.     if (afmFiles[0]==0)
  56.     {
  57.         printf(".AFM file for Plain style not found.\nType:  %s  for help.\n",argv[0]);
  58.         for (index=0;index<4;index++)
  59.             fclose(afmFiles[index]);
  60.         exit(0);
  61.     }
  62.  
  63. /* Okay, check the typeface names. */
  64.  
  65.     for (index=0;index<4;index++)
  66.     {
  67.         if (afmFiles[index]!=0)
  68.         {
  69.             incomingString=freadline(afmFiles[index]);
  70.             if (strcmp(incomingString,"StartFontMetrics 2.0"))
  71.             {
  72.                 printf("%s is not an AFM 2.0 file!  Type  %s  for help.\n",argv[index+1],argv[0]);
  73.                 for (index2=0;index2<4;index2++)
  74.                     fclose(afmFiles[index2]);
  75.                 exit(0);
  76.             }
  77.  
  78.             test=-1;
  79.             while (test!=0)
  80.             {
  81.                 incomingString=freadline(afmFiles[index]);
  82.                 sscanf(incomingString,"%s\n",typefaceNames[index]);
  83.                 test=strcmp(typefaceNames[index],"FontName");
  84.             }
  85.             sscanf(incomingString,"%s %s",typefaceNames[index],typefaceNames[index]);
  86.             fclose(afmFiles[index]);
  87.         }
  88.         else
  89.         {
  90.             strcpy(typefaceNames[index],typefaceNames[0]);
  91.             fclose(afmFiles[index]);
  92.         }
  93.     }
  94.  
  95.     if (strlen(familyName)==0)
  96.         strcpy(familyName,GetMetricFilename(typefaceNames));
  97.  
  98.     if (strlen(familyName)==0)
  99.     {
  100.         printf("Error: These AFMs are from different type families!\n");
  101.         exit(0);
  102.     }
  103.     else
  104.     {
  105.         printf("Typefaces chosen for %s:\n========================================\n",familyName);
  106.         printf("Regular:     %s\n",typefaceNames[0]);
  107.         for (index=1;index<4;index++)
  108.         {
  109.             test=strcmp(typefaceNames[index],typefaceNames[0]);
  110.             if (test==0)
  111.                 strcpy(typefaceNames[index],"(No .AFM Given)");
  112.         }
  113.         printf("Bold:        %s\n",typefaceNames[1]);
  114.         printf("Italic:      %s\n",typefaceNames[2]);
  115.         printf("Bold-Italic: %s\n",typefaceNames[3]);
  116.         printf("========================================\n");
  117.     }
  118.         
  119.  
  120. /* And now the facts are established. We have four or less AFMs
  121.    to make metrics vectors from, and all idiot-proofing is done. */
  122.  
  123.     for (index=0;index<4;index++)
  124.     {
  125.         vectorLengths[index]=WriteMetricsVector(argv[index+1],index+1);
  126.         if (vectorLengths[index]!=0)
  127.             printf("Processed %s\n",typefaceNames[index]);
  128.     }
  129.     printf("========================================\n%s Complete.\n",familyName);
  130.  
  131. /* Now, four or less files are sitting in T: named by their
  132.    typeface names. I generate the .metric by first writing
  133.    five longwords (metric offsets and the file size) followed
  134.    by these four or less files concatenated together. 
  135.    Poof! Instant .metric file! */
  136.  
  137.     metricFile=fopen(familyName,"w");
  138.     if (metricFile==0)
  139.     {
  140.         printf("Unable to create .metric file. \n");
  141.         fclose(metricFile);
  142.         exit(0);
  143.     }
  144.  
  145.     metricOffsets[0]=20;
  146.     for (index=1;index<5;index++)
  147.         metricOffsets[index]=metricOffsets[index-1]+vectorLengths[index-1];
  148.  
  149.     for (index=1;index<4;index++)
  150.         if (vectorLengths[index]==0)
  151.             metricOffsets[index]=0;
  152.  
  153.     for (index=0;index<5;index++)
  154.     {
  155.         singleChars=&metricOffsets[index];
  156.         fprintf(metricFile,"%c%c%c%c",singleChars[0],singleChars[1],singleChars[2],singleChars[3]);
  157.     }
  158.  
  159.     for (index=0;index<4;index++)
  160.     {
  161.         strcpy(vectorFilename,"T:");
  162.         strcat(vectorFilename,typefaceNames[index]);
  163.         metricVectorFile=fopen(vectorFilename,"r");
  164.         for (index2=0;index2<vectorLengths[index];index2++)
  165.         {
  166.             oneChar=fgetc(metricVectorFile);
  167.             fputc(oneChar,metricFile);
  168.         }
  169.         fclose(metricVectorFile);
  170.         remove(vectorFilename);
  171.     } 
  172.  
  173. /* Whew! That's a lot of disk access! By now the .metric
  174.    is complete and the T: files are gone. Time to close up
  175.    and quit. */
  176.  
  177.     fclose(metricFile); 
  178.     exit(0);
  179. };
  180.  
  181. #include "GetMetricFilename.c"
  182. #include "FindBaseName.c"
  183. #include "freadline.c"
  184. #include "WriteMetricsVector.c"
  185.  
  186.